package org.netty.udp.client.start;

import java.net.InetSocketAddress;

import org.netty.udp.client.config.UdpNettyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import io.netty.channel.ChannelFuture;

@Component
@Order(value=1)
public class UdpNettyClientStartRunner implements CommandLineRunner {

	@Value("${udp.netty.server.host}")
    private String host;
    @Value("${udp.netty.server.port}")
    private int port;
    
    @Autowired
    private UdpNettyClient udpclientServer;
	    
	@Override
    public void run(String... args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(host, port);
        ChannelFuture channelFuture = udpclientServer.bing(address);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> udpclientServer.destroy()));
        channelFuture.channel().closeFuture().syncUninterruptibly();
    }

}
